using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._1028
{
    public class UserServiceImpl : IUserService
    {
        // 4. 泛型集合保存注册用户
        private readonly List<User> _users = new List<User>();

        public void Show()
        {
            bool running = true;
            while (running)
            {
                Console.Clear();
                Console.WriteLine("===== 欢迎使用 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择(1-3): ");
                switch (Console.ReadLine()?.Trim())
                {
                    case "1": Login(); break;
                    case "2": Regist(); break;
                    case "3":
                        running = false;
                        Console.WriteLine("程序已退出，感谢使用！");
                        break;
                    default:
                        Console.WriteLine("请输入有效数字！");
                        WaitForKeyPress();
                        break;
                }
            }
        }

        public void Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");
            Console.Write("请输入用户名: ");
            string name = Console.ReadLine()?.Trim();
            Console.Write("请输入密码: ");
            string pwd  = Console.ReadLine()?.Trim();

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd))
            {
                Console.WriteLine("用户名或密码不可为空！");
                WaitForKeyPress();
                return;
            }

            User found = null;
            foreach (var u in _users)          // 5. foreach 迭代
            {
                if (u.UserName == name)
                {
                    found = u;
                    break;
                }
            }

            if (found == null)
                Console.WriteLine("登录失败：用户名不存在！");
            else if (found.Password != pwd)
                Console.WriteLine("登录失败：密码错误！");
            else
                Console.WriteLine($"登录成功！欢迎您，{name}！");

            WaitForKeyPress();
        }

        public void Regist()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");

            string name = GetValidUserName();
            if (name == null) return;

            string pwd = GetValidPassword();
            if (pwd == null) return;

            _users.Add(new User(name, pwd));
            Console.WriteLine("注册成功！");

            // 5. 用 foreach 展示当前全部用户
            Console.WriteLine("当前全部注册用户：");
            foreach (var u in _users)
                Console.WriteLine($"  {u.UserName}");

            WaitForKeyPress();
        }

        #region 私有辅助方法
        private string GetValidUserName()
        {
            while (true)
            {
                Console.Write("请设置用户名: ");
                string name = Console.ReadLine()?.Trim();
                if (string.IsNullOrEmpty(name))
                {
                    Console.WriteLine("用户名不可为空！"); continue;
                }
                if (name.Length < 3 || name.Length > 15)
                {
                    Console.WriteLine("用户名长度必须为3-15个字符！"); continue;
                }
                if (!Regex.IsMatch(name, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("用户名只能包含字母、数字和下划线！"); continue;
                }
                bool exist = false;
                foreach (var u in _users)
                {
                    if (u.UserName == name) { exist = true; break; }
                }
                if (exist)
                {
                    Console.WriteLine("该用户名已被注册，请更换！"); continue;
                }
                return name;
            }
        }

        private string GetValidPassword()
        {
            while (true)
            {
                Console.Write("请设置密码: ");
                string pwd = Console.ReadLine()?.Trim();
                if (string.IsNullOrEmpty(pwd))
                {
                    Console.WriteLine("密码不可为空！"); continue;
                }
                if (pwd.Length < 6 || pwd.Length > 20)
                {
                    Console.WriteLine("密码长度必须为6-20个字符！"); continue;
                }
                if (!Regex.IsMatch(pwd, @"[A-Z]") ||
                    !Regex.IsMatch(pwd, @"[a-z]") ||
                    !Regex.IsMatch(pwd, @"[0-9]"))
                {
                    Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                    continue;
                }

                Console.Write("请确认密码: ");
                string again = Console.ReadLine()?.Trim();
                if (again != pwd)
                {
                    Console.WriteLine("两次输入的密码不一致！"); continue;
                }
                return pwd;
            }
        }

        private void WaitForKeyPress()
        {
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }
        #endregion
    }
}